/*
 * @lc app=leetcode.cn id=989 lang=javascript
 *
 * [989] 数组形式的整数加法
 */

// @lc code=start
/**
 * @param {number[]} num
 * @param {number} k
 * @return {number[]}
 */
var addToArrayForm = function (num, k) {
  num = num.reverse();
  let num2 = [];
  while (k) {
    num2.push(k % 10);
    k = k / 10 >> 0;
  }
  if (num.length < num2.length) {
    [num, num2] = [num2, num];
  }
  let c = 0;
  for (let i = 0; i < num.length; i++) {
    const n = num[i] + (num2[i] ?? 0) + c
    num[i] = n % 10;
    c = n / 10 >> 0;
  }
  if (c) num.push(c);
  return num.reverse();
};
// @lc code=end
